package cn.edu.zjgsu.ito.controller;
import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;
import cn.edu.zjgsu.ito.service.AdminExportClassroomService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;

@Before(AdminControllerInterceptor.class)
public class AdminExportClassroomController extends Controller{
	private AdminExportClassroomService adminExportClassroomService =ServiceFactory.createAdminExportClassroomService();
	private List<Record> records;
	private Record record;
	public static final DateFormat formatter=new SimpleDateFormat("yyyy-MM-dd-HH：mm");
	
	/**
	  *  导出Excel
	 * @throws FileNotFoundException 
	  * 
	  */
 	public void exportExcel() throws FileNotFoundException
 	{
 		Date d=new java.util.Date();
 		
        System.out.println("----------------------------------???????????????????????????????????????");
        String filedir=PathKit.getWebRootPath();
 		filedir=filedir+"\\WEB-INF\\download\\";
 		
        deleteFile(new File(filedir));//清空缓存区
         
 		String classroomType; 		
 		records=adminExportClassroomService.classroom();
// 		record=adminExportClassroomService.TTT();
 		System.out.println(records);
 		System.out.println(record);
 		classroomType=records.get(0).get("classroomType");
 		classroomType=classroomType.replace(" ", "-");
 		classroomType=classroomType+"教室汇总表("+formatter.format(d)+").xls";
 		
 		OutputStream out = new FileOutputStream(filedir+classroomType);	 		
 		System.out.println(filedir+classroomType);
 		adminExportClassroomService.exportExcel(records, out);
 		String outpath=filedir+classroomType;
 		
 		renderFile(new File(outpath));
 	}
 	
 	 public void deleteFile(File oldPath) {
          if (oldPath.isDirectory()) {
           File[] files = oldPath.listFiles();
           for (File file : files) {
             deleteFile(file);
           }
          }else{
            oldPath.delete();
          }
        }

}
